package com.jiandan.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import android.app.Activity;
import com.jiandan.bean.JanDanNew;

public class HtmlParser {

    private String html = "";

    private Activity activity;

    public HtmlParser() {}

    public HtmlParser(String html) {
        this.html = html;
    }

    public HtmlParser(File file) {
        BufferedReader br;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));

            StringBuilder sb = new StringBuilder();
            String tmp;
            while ((tmp = br.readLine()) != null) {
                sb.append(tmp);
            }
            this.html = sb.toString();
        } catch (Exception e) {
            this.html = "";
            e.printStackTrace();
        }
    }

    public String getPageNavi() throws Exception {
        try {
            if (this.html == null || "".equals(this.html)) {
                return null;
            }
            Document doc = Jsoup.parse(this.html);

            Element comments = doc.select("div.cp-pagenavi").first();

            // 第一步获得这里上一页的地址：
            Element pageNavi = comments.select("a.previous-comment-page").first();
            if (pageNavi == null) {
                return null;
            }
            String tmp = pageNavi.attr("href");
            String nextPage = tmp.substring(0, tmp.lastIndexOf('#'));
            return nextPage;
        } catch (Exception e) {
            throw e;
        }
    }

    public void handleImgs(BlockingQueue<String> imageQueue) throws Exception {
        try {
            Document doc = Jsoup.parse(this.html);
            Element commentlist = doc.select("ol.commentlist").first();
            Elements comments = commentlist.select("p>img");
            if (comments.size() < 1) {
                return;
            }
            String src = "";
            for (Element img : comments) {
                src = img.attr("src");
                imageQueue.put(src);
            }
        } catch (Exception e) {
            throw e;
        }
    }

    public LinkedList<String> handleImgs() throws Exception {
        try {
            Document doc = Jsoup.parse(this.html);
            Element commentlist = doc.select("ol.commentlist").first();
            Elements comments = commentlist.select("p>img");
            if (comments.size() < 1) {
                return null;
            }
            LinkedList<String> imgs = new LinkedList<String>();
            String src = "";
            for (Element img : comments) {
                src = img.attr("src");
                imgs.add(src);
            }
            return imgs;
        } catch (Exception e) {
            throw e;
        }
    }

    /**
     * 抓取"http://jandan.net/new"页面标题列表页html代码段
     * 
     * @throws Exception
     */
    public LinkedList<JanDanNew> handleTitles() throws Exception {
        LinkedList<JanDanNew> list = null;
        try {
            Document doc = Jsoup.parse(this.html);
            Element commentlist = doc.select("div.post").first();
            Elements licomments = commentlist.select("li");
            if (licomments != null && licomments.size() >= 1) list = new LinkedList<JanDanNew>();
            for (Element element : licomments) {
                JanDanNew dan = new JanDanNew();
                dan.setDate(element.getElementsByTag("small").first().ownText());
                dan.setUrl(element.getElementsByTag("a").first().attr("href"));
                dan.setTitle(element.getElementsByTag("a").first().ownText());
                dan.setReplyCount(element.getElementsByTag("span").first().attr("title"));
                list.add(dan);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
        return list;
    }


    public String handlerNewDetails() throws Exception {

        String map = null;

        try {

            Document doc = Jsoup.parse(this.html);

            // 得到"<div id="content">"
            Element content = doc.getElementById("content");

            // 得到第一个"<div class="post f">"
            Element post_div = content.select("div.post").first();

            // 删除文章头部所有span标签
            post_div.select("span").remove();

            // 删除发帖用户头像所在a标签-即删除用户头像
            post_div.select("img.avatar").first().remove();

            // 删除文章尾部"embed"和"em"标签
            post_div.select("p > em , p > embed").remove();

            // 删除正文中所有a标签的href属性
            post_div.select("a").removeAttr("href");

            // 设置网页中图片宽度
            post_div.select("img").attr("width", (DensityUtil.controlImgWidth(activity) - 40) + "");

            // 获取评论<ol.commentlist>
            Element comment = doc.select("ol.commentlist").first();

            // 删除投票"oo[]xx[]"
            comment.select("div.vote").remove();

            // 删除a标签下 href和onclick属性
            comment.select("a[href]").removeAttr("href").removeAttr("onclick");

            // 控制评论图片宽度
            comment.select("img").attr("width", (DensityUtil.controlImgWidth(activity) - 60) + "");

            map = post_div.toString() + "<h2><font color=#4B0082>你们就扯吧~~~</font></h2>"+comment.toString();

        } catch (Exception e) {

            e.printStackTrace();

            throw e;
        }

        return map;

    }

    public void setHtml(String html) {
        
        this.html = html;
    }

    public void setActivity(Activity activity) {
        
        this.activity = activity;
    }

}
